package com.bite.demo.proxy;

import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        //目标对象  代理接口
        HouseSubject target = new RealHouseSubject();

        //目标对象2: 代理类
        HouseSubjectClass subjectClass = new HouseSubjectClass();
        //静态代理
//        HouseProxy proxy = new HouseProxy(subject);
//        proxy.rentHouse();

        //JDK动态代理
//        HouseSubject proxy = (HouseSubject) Proxy.newProxyInstance(
//                target.getClass().getClassLoader(),
//                new Class[]{HouseSubject.class},
//                new JDKInvocation(target));
//        proxy.saleHouse();
//        proxy.rentHouse();

//        HouseSubjectClass proxy2 = (HouseSubjectClass) Proxy.newProxyInstance(
//                subjectClass.getClass().getClassLoader(),
//                new Class[]{HouseSubjectClass.class},
//                new JDKInvocation(subjectClass));
//
//        proxy2.rentHouse();

        //CGLib 动态代理
        HouseSubject proxy3 = (HouseSubject) Enhancer.create(target.getClass(), new CGLibIntercepter(target));
        proxy3.rentHouse();

        //CGLib 代理类

        HouseSubjectClass proxy4 = (HouseSubjectClass) Enhancer.create(subjectClass.getClass(), new CGLibIntercepter(subjectClass));
        proxy4.rentHouse();

//        target.rentHouse();

    }
}
